package com.racedepartment.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import com.racedepartment.model.Profil;

public class ProfilDaoImpl extends Dao implements ProfilDao {

	public ProfilDaoImpl(Connection connection) throws SQLException {
		super(connection, Profil.TABLE_NAME, Profil.PROFIL_SK);
	}

	//
	@Override
	public List<Profil> getAllProfiles() {
		List<Profil> list = new LinkedList<Profil>();
		ResultSet rs = null;
		try {
			rs = getAllRecords();
			while (rs.next()) {
				list.add(createProfil(rs));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public Profil getProfil(int profil_sk) {
		ResultSet rs;
		Profil profil = null;
		try {
			rs = getRecord(profil_sk);
			profil = createProfil(rs);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return profil;
	}

	@Override
	public int addProfil(Profil profil) {
		ResultSet rs;
		int pk = -1;
		try {
			rs = insertRecord(profil);
			pk = rs.getInt(1);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return pk;
	}

	@Override
	public void updateProfil(Profil profil) {
		try {
			updateRecord(profil);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void deleteProfil(Profil profil) {
		deleteProfil(profil.getProfil_sk());
	}

	@Override
	public void deleteProfil(int profil_sk) {
		try {
			deleteRecord(profil_sk);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private Profil createProfil(ResultSet rs) throws SQLException {
		int id = rs.getInt(Profil.PROFIL_SK);
		String name = rs.getString(Profil.NAME);
		Profil profil = new Profil(id, name);
		return profil;
	}
	
}
